package com.cindata

import com.cindata.dictionary.*

class FinancialPledgeImport
{
    // 银行承兑汇票表（金融质押品类-票据）

    FinancialPledgeImportBatch batch

    Province province
    City city
    District district
    EstateStatusDict estateStatus
    Boolean isEstate = true
    BigDecimal unitPrice
    // 评估单价
    BigDecimal totalPrice
    // 评估总价
    Date valuationDate
    // 评估时间
    BigDecimal ltv
    // 抵押率 （%）
    BigDecimal cltv
    // 动态抵押率 （%）
    Account account
    // 直属管理机构(银行网点)
    Mortgage mortgage
    // 贷款
    MortgageStatusDict mortgageStatus

    // 定期存单
    Boolean isNativeDeposit = false
    // 是否本行存单
    Boolean isElectronicDeposit = false
    // 是否电子存单
    String financialAccount
    // 存款账号/国债账号
    String depositFullName
    // 存款户名/姓名
    String depositBank
    // 存款银行
    Double rate
    // 存单利率/国债利率/债券年利率（债券利率）

    // 国债
    String agency
    // 代理机构（是否我行承销国债）/债券代理发行机构
    String debtType
    // 国债品种	 1 凭证式 2 记帐式  3、储蓄式国债
    String code
    // 国债代码/债券代码
    String pledgeRegistrationAgency
    // 质押登记机构

    // 债券
    String issuerType
    // 发行机构类型 1．本行 2．中国人民银行 3．国有政策性银行 4．四大国有商业银行 5．已上市其他银行 6．未上市其他银行 7．非银行金融机构
    String issuer
    // 债券发行单位/理财产品发行机构
    Boolean isOrienteeringBond = false
    // 是否为收购国有银行不良贷款而定向发行的债券	下拉列表	手工选择	字符
    String bondAmount
    // 债券数量
    Boolean isRegisteredBond = false
    // 是否记名债券
    Boolean isNeedStopPayment = false
    // 是否需要办理止付
    String incomeType
    // 债券收益类型 1 附息金融债券 2 贴现金融债券
    String chargeInterestMethod
    // 债券计息方式 1 固定利率 2 浮动利率 3 累进利率
    String level
    // 债券评级

    // 票据
    String externalId
    // 质押物编号
    FinancialPledgeTypeDict type
    // 质押物类型
    String name
    // 质押物名称
    String number
    // 汇票号码/存单号码/凭证编号/所有权编号/发票号/保单号码（保单编号）/凭证号码
    String fullName
    // 出质人名称
    ContactIdentityType identityType
    // 出质人证件类型
    String idNumber
    // 出质人证件号码
    String loanCardNumber
    // 出质人贷款卡编号
    String commonOwnerFullName
    // 其他共同所有人
    Double sharePoint
    // 所占份额
    String drawerFullName
    // 出票人名称（出票人）
    String drawerAccountNumber
    // 出票人账号
    String queryAndCheck
    // 查询查复情况  1.已查询查复 2.未查询查复
    Date queryAndCheckDate
    // 查询查复日期
    Boolean isValidate = false
    // 票据真实性审核 1.已审核 2.未审核
    String holderFullName
    // 持票人
    String acceptanceBank
    // 承兑行
    String acceptanceBankNo
    // 承兑行行号
    String endorserFullName
    //背书人
    Date startDate
    // 出票日期/国债开户日/债券发行日/理财产品发行日期/保单生效日
    Integer duration
    // 票据期限/存单期限/国债期限/债券期限/保险期限
    Date endDate
    // 到期日期（票据到期日期）/存单到期日/国债到期日/债券到期日期/理财产品到期日期/保单到期日期
    String acceptanceAccountType
    // 承兑机构类型  1.本行 2.中国人民银行 3.国有政策性银行 4.四大国有商业银行 5.已上市其他银行 6.未上市其他银行 7.非银行金融机构
    Double amount
    // 票据金额/存单金额/国债金额/债券金额/理财产品金额/股票金额/投保金额

    Date interestStartDate
    // 存款起息日/理财产品起息日期
    Date interestEndDate

    // 理财产品
    String productName
    // 理财产品名称/股票/基金名称
    String productCode
    // 股票/基金代码
    String productQuantity
    // 股票数量
    String protocolNumber
    // 协议书编号
    String certificateNumber
    // 权利凭证编号
    Boolean isCapitalProtected = false
    // 是否保本型
    Boolean isRedemption = false
    // 是否可提前赎回
    Date toAccountDate
    // 理财产品到账日期
    Double annualRate
    // 预期年化收益率

    // 股票股权
    String financialInstruments
    // 可转让基金份额投资的金融工具信息
    String trusteeshipAccount
    // 股票托管券商/托管机构
    String accountNature
    // 股票企业性质 “上市公司或非上市公司”。
    Boolean isHighQualityAccount = false
    // 股票企业是否优质国企

    // 贵金属
    Double purity
    // 纯度
    Double weight
    // 重量（克）
    Double purchaseValue
    // 购买价值	文本框	手工输入	金额	否	　
    String storageAddress
    // 存放地点

    // 保单
    Boolean isHavePledgeClause = false
    // 是否有质押条款
    Boolean isAgreedPledgeRate = false
    // 是否约定质押率
    String insurer
    // 保险公司
    String policyHolder
    // 投保人
    String insurant
    // 被保险人
    String beneficiary
    // 受益人
    Integer depositDuration
    // 保险费交缴期限
    String depositType
    // 保险费交缴方式 分期、趸交
    Double depositAmountPerMonth
    // 保险费每期交缴金额

    CurrencyDict currency
    // 币种
    Cashability cashability
    // 变现能力 1.通用且变现容易，2.通用但变现较不易，专用性强
    String valueVolatility
    // 价值波动性 1.价值波动大；2.价值波动中；3.有升值潜力或波动小
    String seizureOfConvenience
    // 查封便利性 1.无查封障碍的不动产，动产由我行监管；2.查封有一定障碍的不动产，动产存放地点相对固定但非我行监管；3.查封有障碍的不动产，动产存放地点难掌握和控制
    String relationOfMortgagor
    // 抵质押物与借款人相关性 1.本人 2.第三方
    String legalValidity
    // 法律有效性 1.担保合法且手续完整 2.担保不合法或手续基本无效
    Double firstEvaluatePrice
    // 初次认定价值
    Account firstEvaluateAccount
    // 初次评估机构
    Account latestEvaluateAccount
    // 最新评估机构
    Double latestEvaluatePrice
    // 最新认定价值
    Date latestEvaluateDate
    // 最新认定日期
    Double actualMortgageRate
    // 实际抵质押率 (实际抵质押率=债权本息总额÷抵押物评估价值（或质押物认定价值）×100%)
    Double mortgageRateUpperLimit
    // 抵质押率上限
    String description
    // 抵质押物说明

    String registerFullName
    // 登记人
    String registerAccount
    // 登记机构
    Date registerDate
    // 登记日期
    Date registerUpdateDate
    // 更新日期

    Date evaluateDate
    // 评估日期
    EvaluateDict evaluateType
    // 1:自动评估 2：人工评估
    ExecStatusDict evaluateStatue
    //评估状态
    String failReason
    // 评估失败原因
    String evaluateCode

    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User user
    User createBy
    User modifyBy

    static mapping = {
        autowire true
    }

    static constraints = {

        province nullable: true, blank: true, comment: '省份'
        city nullable: true, blank: true, comment: '城市'
        district nullable: true, blank: true, comment: '区县'
        estateStatus nullable: true, blank: true, comment: '贷款状态'
        isEstate nullable: true, blank: true, comment: '是否在押'
        unitPrice nullable: true, blank: true, comment: '评估单价'
        totalPrice nullable: true, blank: true, comment: '评估总价'
        valuationDate nullable: true, blank: true, comment: '评估时间'
        ltv nullable: true, blank: true, comment: '抵押率 （%）'
        cltv nullable: true, blank: true, comment: '动态抵押率 （%）'
        account nullable: true, blank: true, comment: '属管理机构(银行网点)'
        mortgage nullable: true, blank: true, comment: '贷款'
        mortgageStatus nullable: true, blank: true, comment: '贷款状态'


        externalId maxSize: 50, nullable: true, blank: true, comment: '质押物编号'
        type nullable: true, blank: true, comment: '质押物类型'
        name maxSize: 50, nullable: true, blank: true, comment: '质押物名称'
        number maxSize: 50, nullable: true, blank: true, comment: '号码'
        fullName maxSize: 20, nullable: true, blank: true, comment: '出质人名称'
        identityType nullable: true, blank: true, comment: '出质人证件类型'
        idNumber maxSize: 18, nullable: true, blank: true, comment: '出质人证件号码'
        loanCardNumber maxSize: 50, nullable: true, blank: true, comment: '出质人贷款卡编号'
        commonOwnerFullName maxSize: 20, nullable: true, blank: true, comment: '其他共同所有人'
        sharePoint nullable: true, blank: true, comment: '所占份额'
        drawerFullName maxSize: 20, nullable: true, blank: true, comment: '出票人名称（出票人）'
        drawerAccountNumber maxSize: 50, nullable: true, blank: true, comment: '出票人账号'
        queryAndCheck maxSize: 200, nullable: true, blank: true, comment: '查询查复情况'
        queryAndCheckDate nullable: true, blank: true, comment: '查询查复日期'
        isValidate nullable: true, blank: true, comment: '票据真实性审核'
        holderFullName maxSize: 20, nullable: true, blank: true, comment: '持票人'
        acceptanceBank maxSize: 50, nullable: true, blank: true, comment: '承兑行'
        acceptanceBankNo maxSize: 50, nullable: true, blank: true, comment: '承兑行行号'
        endorserFullName maxSize: 20, nullable: true, blank: true, comment: '背书人'
        startDate nullable: true, blank: true, comment: '出票日期（票据签发日）'
        duration nullable: true, blank: true, comment: '票据期限'
        endDate nullable: true, blank: true, comment: '到期日期（票据到期日期）'
        acceptanceAccountType maxSize: 50, nullable: true, blank: true, comment: '承兑机构类型'
        amount nullable: true, blank: true, comment: '金额'
        currency nullable: true, blank: true, comment: '币种'

        cashability nullable: true, blank: true, comment: '变现能力'
        valueVolatility maxSize: 50, nullable: true, blank: true, comment: '价值波动性'
        seizureOfConvenience maxSize: 50, nullable: true, blank: true, comment: '查封便利性'
        relationOfMortgagor maxSize: 50, nullable: true, blank: true, comment: '抵质押物与借款人相关性'
        legalValidity maxSize: 50, nullable: true, blank: true, comment: '法律有效性'
        firstEvaluatePrice nullable: true, blank: true, comment: '初次认定价值'
        latestEvaluatePrice nullable: true, blank: true, comment: '最新认定价值'
        latestEvaluateDate nullable: true, blank: true, comment: '最新认定日期'
        actualMortgageRate nullable: true, blank: true, comment: '实际抵质押率'
        mortgageRateUpperLimit nullable: true, blank: true, comment: '抵质押率上限'
        description maxSize: 512, nullable: true, blank: true, comment: '抵质押物说明'

        registerFullName maxSize: 20, nullable: true, blank: true, comment: '登记人'
        registerAccount maxSize: 50, nullable: true, blank: true, comment: '登记机构'
        registerDate nullable: true, blank: true, comment: '登记日期'
        registerUpdateDate nullable: true, blank: true, comment: '更新日期'

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        user nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true

        isNativeDeposit nullable: true, blank: true
        isElectronicDeposit nullable: true, blank: true
        financialAccount maxSize: 50, nullable: true, blank: true
        depositFullName maxSize: 50, nullable: true, blank: true
        depositBank maxSize: 50, nullable: true, blank: true
        rate nullable: true, blank: true

        agency maxSize: 50, nullable: true, blank: true
        debtType maxSize: 50, nullable: true, blank: true
        code maxSize: 50, nullable: true, blank: true
        pledgeRegistrationAgency maxSize: 50, nullable: true, blank: true

        issuerType maxSize: 50, nullable: true, blank: true
        issuer maxSize: 50, nullable: true, blank: true
        isOrienteeringBond nullable: true, blank: true
        bondAmount nullable: true, blank: true
        isRegisteredBond nullable: true, blank: true
        isNeedStopPayment nullable: true, blank: true
        incomeType maxSize: 50, nullable: true, blank: true
        chargeInterestMethod maxSize: 50, nullable: true, blank: true
        level maxSize: 50, nullable: true, blank: true

        interestStartDate nullable: true, blank: true
        interestEndDate nullable: true, blank: true

        productName maxSize: 50, nullable: true, blank: true
        productCode maxSize: 50, nullable: true, blank: true
        productQuantity nullable: true, blank: true
        protocolNumber maxSize: 50, nullable: true, blank: true
        certificateNumber maxSize: 50, nullable: true, blank: true
        isCapitalProtected nullable: true, blank: true
        isRedemption nullable: true, blank: true
        toAccountDate nullable: true, blank: true
        annualRate nullable: true, blank: true

        // 股票股权
        financialInstruments maxSize: 50, nullable: true, blank: true
        trusteeshipAccount maxSize: 50, nullable: true, blank: true
        accountNature maxSize: 50, nullable: true, blank: true
        isHighQualityAccount nullable: true, blank: true

        // 贵金属
        purity nullable: true, blank: true
        weight nullable: true, blank: true
        purchaseValue nullable: true, blank: true
        storageAddress maxSize: 150, nullable: true, blank: true

        // 保单
        isHavePledgeClause nullable: true, blank: true
        isAgreedPledgeRate nullable: true, blank: true
        insurer maxSize: 50, nullable: true, blank: true
        policyHolder maxSize: 50, nullable: true, blank: true
        insurant maxSize: 50, nullable: true, blank: true
        beneficiary maxSize: 50, nullable: true, blank: true
        depositDuration nullable: true, blank: true
        depositType maxSize: 50, nullable: true, blank: true
        depositAmountPerMonth nullable: true, blank: true

        evaluateType nullable: true, blank: true
        evaluateStatue nullable: true, blank: true
        evaluateDate nullable: true, blank: true
        evaluateCode nullable: true, blank: true, maxSize: 50
        firstEvaluateAccount nullable: true, blank: true
        latestEvaluateAccount nullable: true, blank: true

        failReason maxSize: 200, nullable: true, blank: true, comment: '评估失败原因'

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        user nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }

    def afterInsert()
    {
        // def mortgage = Mortgage.CreateFromEstateImport(this)
    }

    def afterUpdate()
    {
        this.updateMatchNum()
    }

    // 评估成功，进入押品表
    def evaluteSuccess()
    {
        def financialPledge = FinancialPledge.findByExternalId(externalId)
        if (!financialPledge)
        {
            financialPledge = new FinancialPledge()
        }
        financialPledge.externalId = this.externalId
        //
        financialPledge.estateStatus = this.estateStatus

        financialPledge.unitPrice = this.unitPrice
        financialPledge.totalPrice = this.totalPrice
        financialPledge.ltv = this.ltv

        financialPledge.account = this.createBy.account
        financialPledge.isEstate = true

        financialPledge.save()

    }

    /*
    * 评估失败
     */

    def evaluteFailure()
    {
        def userCost = UserCost.findBySourceIdAndSourceClass(this.id, this.class.toString())
        if (userCost)
        {
            userCost.finish(this.evaluateStatue, this.failReason)
        }
    }

    // 更新匹配记录数量
    def updateMatchNum()
    {
        this.batch.updateBatchCount()
    }
}
